//读取AD值并转换成F温度值,得出 烤炉温度,肉探针1温度,肉探针2温度

#include "../App_Main.h"
#include "Kalman.h"


#define FILTER_SIZE 30  // 滤波器的大小，可以根据需要调整

uint8_t Calbr_Temp_Flag=0;
uint8_t Change_Mode_Flag=0;

uint16_t AD_Tick_20mS_Count;
uint16_t ADC_Tick_20ms; //ADC采样间隔2ms计数
uint16_t ADVal[3] = {0}; //DW added to have better resolution for temperature
uint32_t Voltage[3];
_RTD RTD[3];
uint8_t RTD1_status, RTD1_cnt = 0, RTD_cnt = 0;
uint16_t OvenTemp_RTD_C = 100;
uint16_t OvenTemp_PROBE_C = 100;
uint8_t Temperature_sign = 0;
extern int32_t grill_temperature;
//函数接口
double (*ADC_Convert_R)(uint32_t ad); //将AD值转换成电压值
void (*TempADC_Delay0ms_Output)(void); //tick 0ms,输出温度值,铂电阻状态

uint16_t Alarm_Tick_1s;
uint8_t LowTemp_Ctrl = 0; //1:重置低温检测过程, 0:继续执行低温检测
uint16_t LowTemp_Step = 0;
uint16_t LowTemp_Count = 0;
uint8_t ch = 0; //探针选择: 0:烤炉探针, 1:肉探针1, 2:肉探针2
float AD_1v = 0;
float battery_vdd = 0; //units: mV
float battery_vdd1= 0; //units: mV
uint16_t Probe1_PtStateDelayCount = 0;
uint16_t Probe2_PtStateDelayCount = 0;
int32_t temp_filter_time = 0;//时间滤波
uint16_t temp_set_updata = 0;
bool filter_fun = 0;
double temp_set_calculation = 0;
int weights[FILTER_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};  // 权重数组，可以根据需要调整
int temp_diff = 0;
int dis_temp_diff = 0;
//Reference voltage: VDD
void ADInit(void)
{
    uint16_t i;

    adc_model_init();
    LowTemp_Ctrl = 0;
    LowTemp_Step = 0;
    LowTemp_Count = 0;
    ADC_Tick_20ms = 330; //默认开机后连续采样
    RTD[0].status = Pt_Normal;
    Temp_Param.oven_Err = Pt_Normal;
    for (i = 0; i < 3300; i++)
    {
        ADSampling();
    }
    //Temp_Param.oven = RTD[0].value;
    if(TempADC_Delay0ms_Output != 0) TempADC_Delay0ms_Output();
}

//Tick 10ms
void ADC_Clock(void)
{
    static uint16_t count = 0;

    if(Probe1_PtStateDelayCount > 0) Probe1_PtStateDelayCount--;
    if(Probe2_PtStateDelayCount > 0) Probe2_PtStateDelayCount--;
    if(ADC_Tick_20ms < 0xffff) ADC_Tick_20ms++;
    if(Alarm_Tick_1s < 0xffff) Alarm_Tick_1s++;
    count++;
    if(count >= 10)
    {
        count = 0;
        AD_Tick_20mS_Count++;
    }
}

//高温报警: 烤炉温度长时间>650°F,Tick 1s
void HighTempAlarm(void)
{
    static uint16_t HighTemp_Count = 0;

    if (grill_temperature >= 650)
    {
        if (HighTemp_Count >= 5)
        { //持续5s,执行高温报警
            RTD[0].status = Pt_HighTemp;
        }
        else HighTemp_Count++;
    }
    else HighTemp_Count = 0;
}

//低温报警功能: 烤炉温度长时间低于120°F
void LowTempAlarm(void)
{
//    if(lcd_show_config.ShowBit == 0)
//    {
//        LowTemp_Count = 0;
//        LowTemp_Step = 0;
//        LowTemp_Ctrl = 0;
//        return;
//    }

    if (Alarm_Tick_1s >= 100)
    { //Tick 1s
        Alarm_Tick_1s -= 100;

        if(LowTemp_Count < 0xfff0) LowTemp_Count++;
        if ((LowTemp_Count >= 240) && (LowTemp_Step == 0))
        { //开机上电后延迟 240s
            LowTemp_Count = 0;
            LowTemp_Step = 1;
			LowTemp_Ctrl = 1;
        }
        else if(LowTemp_Step == 1)
        {
            if (RTD[0].value < 120)
            {
                if ((LowTemp_Count >= 600) && (LowTemp_Ctrl == 1))
                { //持续600s,执行低温报警
                    RTD[0].status = Pt_LowTemp;
                }
                else if((LowTemp_Count >= 600) && (LowTemp_Ctrl != 1))
                {
                    LowTemp_Count = 0;
                    LowTemp_Ctrl = 1;
                    LowTemp_Step = 0;
                }
            }
            else
            {
                LowTemp_Step = 1;
                LowTemp_Count = 0;
            }
        }
        HighTempAlarm();
    }
}

void ADC_KeyUpDown(void)
{
    #if filter_fun				   
        Calbr_Temp_Flag = 0;
        Change_Mode_Flag = 1;
    #endif
}

// 加权平均滤波函数
double weighted_average_filter() 
{
	
	double sum = 0.0;

    for(int i = 0; i < FILTER_SIZE - 1; i++) 
	{
		if (weights[i] == 0)
		{
			weights[i] = temp_set;
		}
	}
	for (int i = 0; i < FILTER_SIZE - 1; i++) 
	{
		weights[i] = weights[i + 1];
	}
	// 在最后一个位置补充新的温度值
	weights[FILTER_SIZE - 1] = temp_set;
	// 计算温度
	for (int i = 0; i < FILTER_SIZE; i++) 
	{
		sum += weights[i];
	}
	// 返回平均值
	return sum / FILTER_SIZE;
}

void ADSampling(void)
{
    float temp_float;
	static double temp_calculation =0;
    float temp = 0;
	
    if(ADC_Tick_20ms >= 2)
    {
        ADC_Tick_20ms -= 2;

        if(ch == 0)
        {
            temp = SamplingSingle(ADC_INTVREF_1v);
            if(temp == 0) return; 
            AD_1v = ((float)1000.0 / temp); 

            temp = SamplingSingle(ADC_BAT_V);
            temp = AD_1v * temp; //分压
            battery_vdd = temp * 4;
            
            temp = SamplingSingle(ADC_Oven);
        }
        else if(ch == 1) temp = SamplingSingle(ADC_Meat1);
        else if(ch == 2) temp = SamplingSingle(ADC_Meat2);
        
        temp = AD_1v * temp; //分压
		battery_vdd1 = temp;
        temp_float = V_Convert_R(battery_vdd, temp);
        temp_float = Quadratic_F(temp_float);

        if(ch == 0)
        {
            temp_float = kalmanFilter(&KFP_ADC_RTD, temp_float);
            temp_float = MedianFilter(temp_float);
        }
        else if(ch == 1)
        {
            if(Probe1_PtStateDelayCount == 0)
            {
                temp_float = kalmanFilter(&KFP_ADC_Probe1, temp_float);
                temp_float = MedianFilterA(temp_float);
            }
            else kalmanFilter(&KFP_ADC_Probe1, temp_float);
        }
        else if(ch == 2)
        {
            if(Probe2_PtStateDelayCount == 0)
            {
                temp_float = kalmanFilter(&KFP_ADC_Probe2, temp_float);
                temp_float = MedianFilterB(temp_float);
            }
            else kalmanFilter(&KFP_ADC_Probe2, temp_float);
        } 

        if(Pt1000_State == Pt1000_Short)
        {
            RTD[ch].status = Pt_Short;
            if(ch == 1) Probe1_PtStateDelayCount = 150; 
            if(ch == 2) Probe2_PtStateDelayCount = 150; 
        }
        else if(Pt1000_State == Pt1000_Open)
        {
            RTD[ch].status = Pt_Open;
            if(ch == 1) Probe1_PtStateDelayCount = 150; 
            if(ch == 2) Probe2_PtStateDelayCount = 150; 
        }
        else if(ch != 0)
        {
            if((ch == 1) && (Probe1_PtStateDelayCount == 0)) RTD[ch].status = Pt_Normal;
            if((ch == 2) && (Probe2_PtStateDelayCount == 0)) RTD[ch].status = Pt_Normal;
        }
        if(temp_float > 999) temp_float = 999; //999 ℉
        RTD[ch].value = temp_float;
        ch++;
        if (ch >= 3) ch = 0;

		//RTD温度相对于烤网温度进行补偿
		if(RTD[0].value>100)
		{
			grill_temperature = RTD[0].value+((RTD[0].value-100)/5);
		}			   
		else
		{
			grill_temperature = RTD[0].value;
		}
		
		temp_diff = (int)temp_set - grill_temperature;

		if(filter_fun)
		{	
			if(temp_set_updata > 10)
			{
				temp_set_updata = 1;
				temp_set_calculation = weighted_average_filter();
			}
			if((grill_temperature-temp_set_calculation<50)&&(grill_temperature-temp_set_calculation>-50))
			{
				dis_temp_diff = (grill_temperature-temp_set_calculation)/2;
			}
			else 
			{
				if((grill_temperature-temp_set_calculation)>=50)
				{
					dis_temp_diff = 25;
				}
				else if((grill_temperature-temp_set_calculation)<=-50)
				{
					dis_temp_diff = -25;
				}
			}
			if(temp_filter_time<300)
			{
				temp_calculation = grill_temperature - (int)(((temp_filter_time*temp_filter_time)*(dis_temp_diff))/(300*300));
			}
			else
			{
				temp_calculation = grill_temperature - dis_temp_diff;
			}

			Temp_Param.oven = temp_calculation;
			
		}
		else
		{
			Temp_Param.oven = grill_temperature;
		}

    }
}

void TempADC_Main(void)
{
    ADSampling();

    if(TempADC_Delay0ms_Output != 0) TempADC_Delay0ms_Output();
    if(RTD[0].status > 0)
    { //烤炉探针出错
        LowTemp_Ctrl = 0; //延迟低盐检测10min
    }
}
